﻿using System;

namespace assignment3
{
    class program_1
    {

        public abstract class Shape
        {

            abstract public double quadrature();
            abstract public bool isValid();
        }

        public class Rectangle : Shape
        {
            public double l;
            public double w;

            public Rectangle(double l,double w)
            {
                this.l = l;
                this.w = w;
            }

            public override bool isValid()
            {
                return l > 0 && w > 0;
            }

            public override double quadrature()
            {
                if(isValid())
                {
                    return l * w;
                }
                else
                {
                    return 0;
                }
                
            }


        }

        public class Square : Shape
        {
            public double a;

            public Square(double a)
            {
                this.a = a;
            }

            public override bool isValid()
            {
                return a > 0;
            }

            public override double quadrature()
            {
                if(isValid())
                {
                    return a * a;
                }
                else
                {
                    return 0;
                }
                
            }
        }

        public class Triangle : Shape
        {
            public double a;
            public double b;
            public double c;

            public Triangle(double a,double b,double c)
            {
                this.a = a;
                this.b = b;
                this.c = c;
            }
            public override bool isValid()
            {
                return a > 0 && b > 0 && c > 0 && a + c > b && a + b > c && b + c > a;
            }

            public override double quadrature()
            {
                double s = (a + b + c) / 2;
                
                if (isValid())
                {
                    return Math.Sqrt(s * (s - a) * (s - b) * (s - c));
                }
                else
                {
                    return 0;
                }
            }
        }


        public class ShapeFactory
        {
            public Random num = new Random();

            public Shape CreateRandomShape()
            {
                int type = num.Next(0, 3); // 0: Rectangle, 1: Square, 2: Triangle

                switch (type)
                {
                    case 0:
                        return new Rectangle(
                            num.NextDouble() * 10 + 1,
                            num.NextDouble() * 10 + 1);
                    case 1:
                        return new Square(num.NextDouble() * 10 + 1);
                    case 2:
                        double a = num.NextDouble() * 10 + 1;
                        double b = num.NextDouble() * 10 + 1;
                        double cMin = Math.Abs(a - b);
                        double cMax = a + b;
                        double c = cMin + num.NextDouble() * (cMax - cMin);
                        return new Triangle(a, b, c);
                    default:
                        return new Triangle(0, 0, 0);
                }
            }
        }

        static void Main()
        {
            ShapeFactory factory = new ShapeFactory();
            List<Shape> shapeArr = new List<Shape>();

            for (int i = 0; i < 10; i++)
            {
                shapeArr.Add(factory.CreateRandomShape());
            }

            double sum = 0;
            foreach (var shape in shapeArr)
            {
                sum += shape.quadrature();
            }

            Console.WriteLine($"10个随机图形的面积之和是{sum}");
        }
    }
}
